package com.zhumei.keyscale.widget;

import me.jingbin.bymvvm.base.*;
import android.content.*;
import android.graphics.*;
import android.util.*;
import androidx.recyclerview.widget.*;
import android.os.*;
import android.view.*;
import androidx.core.view.*;

public class TvRecyclerView extends RecyclerView
{
    private static final String TAG = "TvRecyclerView";
    private BaseActivity mBindFragment;
    private int mPosition;
    
    public TvRecyclerView(final Context context) {
        this(context, null);
    }
    
    public TvRecyclerView(final Context context, final AttributeSet set) {
        this(context, set, -1);
    }
    
    public TvRecyclerView(final Context context, final AttributeSet set, final int n) {
        super(context, set, n);
        this.init(context, set, n);
    }
    
    private int getFreeHeight() {
        return this.getHeight() - this.getPaddingTop() - this.getPaddingBottom();
    }
    
    private int getFreeWidth() {
        return this.getWidth() - this.getPaddingLeft() - this.getPaddingRight();
    }
    
    private void init(final Context context, final AttributeSet set, final int n) {
        this.initView();
    }
    
    private void initView() {
        this.setDescendantFocusability(262144);
        this.setHasFixedSize(true);
        this.setWillNotDraw(true);
        this.setOverScrollMode(2);
        this.setChildrenDrawingOrderEnabled(true);
        this.setClipChildren(false);
        this.setClipToPadding(false);
        this.setClickable(false);
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        this.setItemAnimator(null);
    }
    
    private boolean isVertical() {
        final LayoutManager layoutManager = this.getLayoutManager();
        boolean b = false;
        if (layoutManager != null) {
            b = b;
            if (((LinearLayoutManager)this.getLayoutManager()).getOrientation() == 1) {
                b = true;
            }
        }
        return b;
    }
    
    private boolean isVisBottom(final RecyclerView recyclerView) {
        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
        final int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
        final int childCount = ((LayoutManager)linearLayoutManager).getChildCount();
        final int itemCount = ((LayoutManager)linearLayoutManager).getItemCount();
        return childCount > 0 && lastVisibleItemPosition == itemCount - 1;
    }
    
    protected int computeScrollDeltaToGetChildRectOnScreen(final Rect rect) {
        return 0;
    }
    
    public boolean dispatchKeyEvent(final KeyEvent keyEvent) {
        final boolean dispatchKeyEvent = super.dispatchKeyEvent(keyEvent);
        final View focusedChild = this.getFocusedChild();
        final BaseActivity mBindFragment = this.mBindFragment;
        if (mBindFragment != null) {
            mBindFragment.setCacheViewFromContent(focusedChild);
        }
        if (focusedChild == null) {
            return dispatchKeyEvent;
        }
        if (keyEvent.getAction() == 1) {
            return true;
        }
        switch (keyEvent.getKeyCode()) {
            default: {
                return dispatchKeyEvent;
            }
            case 22: {
                final View nextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup)this, focusedChild, 66);
                if (nextFocus != null) {
                    nextFocus.requestFocus();
                    return true;
                }
                return false;
            }
            case 21: {
                final View nextFocus2 = FocusFinder.getInstance().findNextFocus((ViewGroup)this, focusedChild, 17);
                final StringBuilder sb = new StringBuilder();
                sb.append("leftView is null:");
                sb.append(nextFocus2 == null);
                Log.i("TvRecyclerView", sb.toString());
                if (nextFocus2 != null) {
                    nextFocus2.requestFocus();
                    return true;
                }
                return false;
            }
            case 20: {
                if (this.isVisBottom(this)) {
                    this.smoothScrollToPosition(this.getLastVisiblePosition());
                    return dispatchKeyEvent;
                }
                final View nextFocus3 = FocusFinder.getInstance().findNextFocus((ViewGroup)this, focusedChild, 130);
                final StringBuilder sb2 = new StringBuilder();
                sb2.append(" downView is null:");
                sb2.append(nextFocus3 == null);
                Log.i("TvRecyclerView", sb2.toString());
                if (nextFocus3 != null) {
                    nextFocus3.requestFocus();
                    this.smoothScrollBy(0, nextFocus3.getTop() + nextFocus3.getHeight() / 2 - this.getHeight() / 2);
                }
                return true;
            }
            case 19: {
                final View nextFocus4 = FocusFinder.getInstance().findNextFocus((ViewGroup)this, focusedChild, 33);
                final StringBuilder sb3 = new StringBuilder();
                sb3.append("upView is null:");
                sb3.append(nextFocus4 == null);
                Log.i("TvRecyclerView", sb3.toString());
                if (nextFocus4 != null) {
                    nextFocus4.requestFocus();
                    this.smoothScrollBy(0, -(this.getHeight() / 2 - (nextFocus4.getBottom() - nextFocus4.getHeight() / 2)));
                    return true;
                }
                Log.i("TvRecyclerView", "tab cache view");
                final BaseActivity mBindFragment2 = this.mBindFragment;
                if (mBindFragment2 != null) {
                    mBindFragment2.setmCacheViewFromTab(focusedChild);
                }
                return dispatchKeyEvent;
            }
        }
    }
    
    @Override
    public int getBaseline() {
        return -1;
    }
    
    @Override
    protected int getChildDrawingOrder(int n, final int mPosition) {
        final View focusedChild = this.getFocusedChild();
        if (focusedChild != null) {
            final int mPosition2 = this.getChildAdapterPosition(focusedChild) - this.getFirstVisiblePosition();
            if ((this.mPosition = mPosition2) < 0) {
                return mPosition;
            }
            --n;
            if (mPosition == n) {
                if (mPosition2 > mPosition) {
                    this.mPosition = mPosition;
                }
                return this.mPosition;
            }
            if (mPosition == mPosition2) {
                return n;
            }
        }
        return mPosition;
    }
    
    public int getFirstVisiblePosition() {
        if (this.getChildCount() == 0) {
            return 0;
        }
        return this.getChildAdapterPosition(this.getChildAt(0));
    }
    
    public int getLastVisiblePosition() {
        final int childCount = this.getChildCount();
        if (childCount == 0) {
            return 0;
        }
        return this.getChildAdapterPosition(this.getChildAt(childCount - 1));
    }
    
    public boolean hasFocus() {
        return super.hasFocus();
    }
    
    public boolean isBottomEdge(int spanCount) {
        final LayoutManager layoutManager = this.getLayoutManager();
        final boolean b = layoutManager instanceof GridLayoutManager;
        boolean b2 = false;
        if (b) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
            final int itemCount = ((LayoutManager)gridLayoutManager).getItemCount();
            final int spanCount2 = gridLayoutManager.getSpanCount();
            int i = 0;
            int n = 0;
            while (i <= spanCount) {
                n += spanSizeLookup.getSpanSize(i);
                ++i;
            }
            if (this.isVertical()) {
                if ((spanCount = itemCount % spanCount2) == 0) {
                    spanCount = gridLayoutManager.getSpanCount();
                }
                if (n > itemCount - spanCount) {
                    return true;
                }
            }
            else if (n % spanCount2 == 0) {
                return true;
            }
        }
        else if (layoutManager instanceof LinearLayoutManager) {
            if (this.isVertical()) {
                if (spanCount == this.getLayoutManager().getItemCount() - 1) {
                    b2 = true;
                }
                return b2;
            }
            return true;
        }
        return false;
    }
    
    public boolean isInTouchMode() {
        if (Build$VERSION.SDK_INT == 19) {
            return !this.hasFocus() || super.isInTouchMode();
        }
        return super.isInTouchMode();
    }
    
    public boolean isLeftEdge(final int n) {
        final LayoutManager layoutManager = this.getLayoutManager();
        final boolean b = layoutManager instanceof GridLayoutManager;
        final boolean b2 = false;
        boolean b3;
        if (b) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
            final int spanCount = gridLayoutManager.getSpanCount();
            int i = 0;
            int n2 = 0;
            while (i <= n) {
                n2 += spanSizeLookup.getSpanSize(i);
                ++i;
            }
            if (this.isVertical()) {
                b3 = b2;
                if (n2 % gridLayoutManager.getSpanCount() == 1) {
                    return true;
                }
            }
            else {
                b3 = b2;
                if (n2 <= spanCount) {
                    return true;
                }
            }
        }
        else {
            b3 = b2;
            if (layoutManager instanceof LinearLayoutManager) {
                if (this.isVertical()) {
                    return true;
                }
                b3 = b2;
                if (n == 0) {
                    b3 = true;
                }
            }
        }
        return b3;
    }
    
    public boolean isRightEdge(int n) {
        final LayoutManager layoutManager = this.getLayoutManager();
        final boolean b = layoutManager instanceof GridLayoutManager;
        final boolean b2 = false;
        boolean b3;
        if (b) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
            final int spanCount = gridLayoutManager.getSpanCount();
            final int itemCount = ((LayoutManager)gridLayoutManager).getItemCount();
            int i = 0;
            int n2 = 0;
            while (i <= n) {
                n2 += spanSizeLookup.getSpanSize(i);
                ++i;
            }
            if (this.isVertical()) {
                b3 = b2;
                if (n2 % gridLayoutManager.getSpanCount() == 0) {
                    return true;
                }
            }
            else {
                n = itemCount % spanCount;
                if (n == 0) {
                    n = spanCount;
                }
                b3 = b2;
                if (n2 > itemCount - n) {
                    return true;
                }
            }
        }
        else {
            b3 = b2;
            if (layoutManager instanceof LinearLayoutManager) {
                if (this.isVertical()) {
                    return true;
                }
                b3 = b2;
                if (n == this.getLayoutManager().getItemCount() - 1) {
                    b3 = true;
                }
            }
        }
        return b3;
    }
    
    public boolean isTopEdge(final int n) {
        final LayoutManager layoutManager = this.getLayoutManager();
        final boolean b = layoutManager instanceof GridLayoutManager;
        boolean b2 = false;
        if (b) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
            final int spanCount = gridLayoutManager.getSpanCount();
            int i = 0;
            int n2 = 0;
            while (i <= n) {
                n2 += spanSizeLookup.getSpanSize(i);
                ++i;
            }
            if (this.isVertical()) {
                if (n2 <= spanCount) {
                    return true;
                }
            }
            else if (n2 % spanCount == 1) {
                return true;
            }
        }
        else if (layoutManager instanceof LinearLayoutManager) {
            if (this.isVertical()) {
                if (n == 0) {
                    b2 = true;
                }
                return b2;
            }
            return true;
        }
        return false;
    }
    
    @Override
    protected void onDetachedFromWindow() {
        if (this.getLayoutManager() != null) {
            super.onDetachedFromWindow();
        }
    }
    
    protected void onFocusChanged(final boolean b, final int n, final Rect rect) {
        super.onFocusChanged(b, n, rect);
    }
    
    @Override
    public boolean onInterceptTouchEvent(final MotionEvent motionEvent) {
        return super.onInterceptTouchEvent(motionEvent);
    }
    
    @Override
    public void requestChildFocus(final View view, final View view2) {
        super.requestChildFocus(view, view2);
    }
    
    @Override
    public boolean requestChildRectangleOnScreen(final View view, final Rect rect, final boolean b) {
        final int paddingLeft = this.getPaddingLeft();
        final int width = this.getWidth();
        final int paddingRight = this.getPaddingRight();
        final int paddingTop = this.getPaddingTop();
        final int height = this.getHeight();
        final int paddingBottom = this.getPaddingBottom();
        final int n = view.getLeft() + rect.left;
        final int n2 = view.getTop() + rect.top;
        final int width2 = rect.width();
        final int height2 = rect.height();
        final int n3 = n - paddingLeft;
        int n4 = Math.min(0, n3);
        final int n5 = width2 + n - (width - paddingRight);
        final int max = Math.max(0, n5);
        final int n6 = n2 - paddingTop;
        int n7 = Math.min(0, n6);
        final int max2 = Math.max(0, height2 + n2 - (height - paddingBottom));
        final boolean canScrollHorizontally = this.getLayoutManager().canScrollHorizontally();
        final boolean canScrollVertically = this.getLayoutManager().canScrollVertically();
        if (canScrollHorizontally) {
            if (ViewCompat.getLayoutDirection((View)this) == 1) {
                if (max != 0) {
                    n4 = max;
                }
                else {
                    n4 = Math.max(n4, n5);
                }
            }
            else if (n4 == 0) {
                n4 = Math.min(n3, max);
            }
        }
        else {
            n4 = 0;
        }
        if (canScrollVertically) {
            if (n7 == 0) {
                n7 = Math.min(n6, max2);
            }
        }
        else {
            n7 = 0;
        }
        if (n4 == 0 && n7 == 0) {
            return false;
        }
        if (b) {
            this.scrollBy(n4, n7);
        }
        else {
            this.smoothScrollBy(n4, n7);
        }
        this.postInvalidate();
        return true;
    }
    
    public void setBindFragment(final BaseActivity mBindFragment) {
        this.mBindFragment = mBindFragment;
    }
    
    @Override
    public void setLayoutManager(final LayoutManager layoutManager) {
        super.setLayoutManager(layoutManager);
    }
    
    public interface OnInterceptListener
    {
        boolean onIntercept(final KeyEvent p0);
    }
}
